package com.ww.boot.mybatis.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcel;
import com.ww.boot.mybatis.bo.QinQuanBO;
import com.ww.boot.mybatis.entity.BullfrogQinQuanEntity;
import com.ww.boot.mybatis.entity.SkuEntity;
import com.ww.boot.mybatis.entity.SkuInfringeEntity;
import com.ww.boot.mybatis.enums.InfringementRiskLevelEnum;
import com.ww.boot.mybatis.enums.PlatformCodeEnum;
import com.ww.boot.mybatis.mapper.bullfrog.BullfrogMapper;
import com.ww.boot.mybatis.mapper.pms.PmsMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2024-04-24 16:15
 */
//@AllArgsConstructor
@RestController
@RequestMapping("/qinquan")
public class QinQuanController {

    @Autowired
    private BullfrogMapper bullfrogMapper;
    @Autowired
    private PmsMapper pmsMapper;

    @RequestMapping("diff")
    public String diff() {
        Long maxId = 0L;
        List<SkuEntity> list = pmsMapper.getList(maxId);
        List<QinQuanBO> boList = new ArrayList<>();

        while (ObjectUtil.isNotEmpty(list)) {
            list.forEach(skuEntity -> {
                List<BullfrogQinQuanEntity> qunQuanList = bullfrogMapper.getQunQuan(skuEntity.getNwSku());
                if(ObjectUtil.isEmpty(qunQuanList)){
                    return;
                }
                List<SkuInfringeEntity> oldList = qunQuanList.stream().map(qunQuan -> {
                    SkuInfringeEntity skuInfringeEntity = new SkuInfringeEntity();
                    skuInfringeEntity.setInfringementType(qunQuan.getPqsdQinquanType());
                    skuInfringeEntity.setPlatform(PlatformCodeEnum.retrieval(qunQuan.getPqsdPlatform()).getValue());
                    skuInfringeEntity.setQinQuanSource(qunQuan.getPqsdQinquanSource());
                    //风险等级
                    if(ObjectUtil.isNotEmpty(qunQuan.getPqsdQinquanSource()) && qunQuan.getPqsdQinquanSource().indexOf(InfringementRiskLevelEnum.HIGH.getName())!=-1){
                        skuInfringeEntity.setRiskLevel(InfringementRiskLevelEnum.HIGH.getCode());
                    }else{
                        skuInfringeEntity.setRiskLevel(InfringementRiskLevelEnum.NONE.getCode());
                    }
                    return skuInfringeEntity;
                }).collect(Collectors.toList());

                List<SkuInfringeEntity> newList = pmsMapper.getInfringeList(skuEntity.getProductSku());
                if(oldList.size() > newList.size()){
                    System.out.println(String.format("sku %s-%s 牛蛙侵权数%s 新系统侵权数%s", skuEntity.getNwSku(), skuEntity.getProductSku(), qunQuanList.size(), newList.size()));
                    QinQuanBO qinQuanBO = new QinQuanBO();
                    qinQuanBO.setNewList(newList);
                    qinQuanBO.setOldList(oldList);
                    qinQuanBO.setProductSku(skuEntity.getProductSku());
                    qinQuanBO.setSku(skuEntity.getNwSku());
                    boList.add(qinQuanBO);
                }
            });
            maxId = list.get(list.size() - 1).getId();
            list = pmsMapper.getList(maxId);
            System.out.println(boList.size());
        }
        EasyExcel.write(new File("C:\\Users\\ZH500\\桌面\\test\\qinquan.xlsx"), QinQuanBO.class).sheet("xxx").doWrite(boList);
        System.out.println(boList);
        return "成功";
    }
}
